import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2022-09-17
 * Time:15:52
 */

/**
 * 力扣1624:两个相同字符之间的最长字符串
 */

public class maxLengthBetweenEqualCharacters {
    public int maxLengthBetweenEqualCharacters(String s) {
        int[] wordNum = new int[26];
        //将wordNum中的各个下标的值都变为-1
        Arrays.fill(wordNum,-1);
        int max = -1;
        for (int i = 0; i < s.length(); i++) {
            if (wordNum[s.charAt(i) - 'a'] < 0){
                //如果是负数就代表第一次赋予
                wordNum[s.charAt(i) - 'a'] = i;
            }else{
                //当不为负数时说明碰到了相同的数了
                max = Math.max(max,i - wordNum[s.charAt(i) - 'a'] - 1);
            }
        }
        return max;
    }
}
